﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PQT.API;
using PQT.Common;
using PQT.Controls;
using PQT.API.File;
using System.Data;
using System.Data.SqlClient;
using cbmobile.BLC;
using cbmobile.DAC;

namespace cbmobile.UserControl
{
    public partial class TinTuc_Entry : System.Web.UI.UserControl
    {
        BLC_TinTuc blc = new BLC_TinTuc();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"] != null)
                {
                    switch (Request.QueryString["action"].ToLower())
                    { 
                        case "update":
                            BindInfoNews();
                            break;
                    }
                }
            }
        }

        protected void BindInfoNews()
        {
            TinTuc enP = blc.Get_News_By_ID(ID_News);
            DataTable dt=blc.Views(ID_News);
            if (enP != null)
            {
                txtTieuDe.Text = enP.TieuDe;
                txtMoTa.Text = enP.MoTaNgan;
                txtNgayNhap.Text = enP.NgayTao.ToString();
                ddlNewsStatus.SelectedValue = enP.TrangThai.ToString();
                fckmlDescription.Value = enP.NoiDung;
            }

        }


        protected void InsertNews(string TieuDe, int TrangThai, string MoTaNgan, string NoiDung, DateTime NgayTao)
        {
            if (blc.InsertNews(TieuDe, TrangThai, MoTaNgan, NoiDung, NgayTao) == true)
            {
                Alert.Show("Thêm thành công...!");
                Response.Redirect("/RenderMain.aspx?muid=TinTuc_List");
            }
            else
                Alert.Show("Thêm thất bại");
        }

        protected void UpdateNews(int ID_News, string TieuDe, int TrangThai, string MoTaNgan, string NoiDung, DateTime NgayTao)
        {
            if (blc.UpdateNews(ID_News, TieuDe, TrangThai, MoTaNgan, NoiDung, NgayTao) == true)
            {
                Alert.Show("Sửa thành công...!");
                Response.Redirect("/RenderMain.aspx?muid=TinTuc_List");
            }
            else
                Alert.Show("Sửa thất bại");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //int ID_News = blc.id_news;
            string TieuDe = txtTieuDe.Text;
            int TrangThai =Convert.ToInt32(ddlNewsStatus.SelectedValue);
            string MoTaNgan = txtMoTa.Text;
            string NoiDung = fckmlDescription.Value;
            DateTime NgayTao =Convert.ToDateTime(txtNgayNhap.Text);
            //if (!IsPostBack)
            //{
                if (Request.QueryString["action"] != null)
                {
                    switch (Request.QueryString["action"].ToLower())
                    {
                        case "update":
                            UpdateNews(ID_News, TieuDe, TrangThai, MoTaNgan, NoiDung, NgayTao);
                            break;
                        case "addnew":
                            InsertNews(TieuDe, TrangThai, MoTaNgan, NoiDung, NgayTao);
                            break;
                    }
                }
            //}
        }

        protected void BindgridView()
        { 
            
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.Calendar1.Visible = !this.Calendar1.Visible;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            this.txtNgayNhap.Text = Calendar1.SelectedDate.ToString();
            this.Calendar1.Visible = false;
        }

        protected int ID_News
        {
            get
            {
                if (Request.QueryString["newId"]!=null)
                    return Convert.ToInt32(Request.QueryString["newId"]);
                return -1;
            }
            set
            {
                
            }
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            Response.Redirect("/RenderMain.aspx?muid=TinTuc_List");
        }
    }
}